package vlms.servlet;

import java.io.IOException;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.Movie;
import vlms.service.VLMSServiceProxy;

/**
 * Servlet implementation class HandleUpdateMovie
 */
public class HandleUpdateMovie extends HttpServlet {
	private static final long serialVersionUID = 1L;
	VLMSServiceProxy proxy = new VLMSServiceProxy();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HandleUpdateMovie() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
proxy.setEndpoint("http://localhost:8080/VLMS/services/VLMSService");
		
		try
		{
		
		System.out.println("in updatemovie servlet");
		String movieId=request.getParameter("id");
		String moviename = request.getParameter("movieName");
		String movieBanner=request.getParameter("movieBanner");
		String rentAmount = request.getParameter("rentAmount");
		String releaseDate = request.getParameter("releaseDate");
		String categories="HORROR";
		 String categoriesValues[]=request.getParameterValues("categories"); 
		String copiesAvailable = request.getParameter("copiesAvailable");
		
		if(categoriesValues!=null)
		{
			categories=categoriesValues[0];
		}
		System.out.println(categories);
		
		Movie movie=new Movie();
		movie.setMovieid(Integer.parseInt(movieId));
		movie.setMoviename(moviename);
		movie.setMoviebanner(movieBanner);
		movie.setCategory(categories);
		
		Calendar  cal = Calendar.getInstance();
		try {
			cal.setTime(new SimpleDateFormat("yyyy-mm-dd").parse(releaseDate));
		} catch (ParseException e) {
			
			request.setAttribute("message", "Invalid Date format");
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/createMovie.jsp");
			dispatcher.forward(request, response);
		}
		
		movie.setReleasedate(cal);
		movie.setCopiesavailable(Integer.parseInt(copiesAvailable));
		movie.setRentamount(Float.parseFloat(rentAmount));
		
		String message=proxy.updateMovie(movie);
		request.setAttribute("message", message);
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/adminHome.jsp");
		dispatcher.forward(request, response);
		}catch (RemoteException e) {
			// TODO Auto-generated catch block
			String message=e.getMessage();
			e.printStackTrace();
			request.setAttribute("message", message);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/createMovie.jsp");
			dispatcher.forward(request, response);
		}
		catch (Exception e) {
			// TODO Auto-generated catch block
			String message=e.getMessage();
			e.printStackTrace();
			request.setAttribute("message", message);
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/createMovie.jsp");
			dispatcher.forward(request, response);
		}
		
	}

}
